﻿using BarrageGameAssistant.Core.Configs;

namespace BarrageGameAssistant.UIForms
{
    public partial class FrmCreateGameConfig : Form
    {

        /// <summary>
        /// 程序配置
        /// </summary>
        public Settings? Settings { get; set; }

        /// <summary>
        /// 游戏配置
        /// </summary>
        public GameProject? Project { get; set; }

        public FrmCreateGameConfig()
        {
            InitializeComponent();
        }

        private void FrmCreateGameConfig_Load(object sender, EventArgs e)
        {
            this.cbbPlatform.SelectedIndex = 0;
            this.cbbGameCategory.SelectedIndex = 0;

            if (Project != null)
            {
                this.txtGameName.ReadOnly = true;
                this.cbbPlatform.SelectedIndex = Project.Platform;
                this.cbbPlatform.Enabled = false;
                this.txtGameName.Text = Project.Name;
                this.txtProcessName.Text = Project.ProcessName;
                this.cbbGameCategory.SelectedIndex = Project.GameCategory;
            }
        }

        private void btnSelectProcessName_Click(object sender, EventArgs e)
        {
            FrmSelectProcess frmSelectProcess = new FrmSelectProcess();
            if (frmSelectProcess.ShowDialog() == DialogResult.OK)
            {
                this.txtProcessName.Text = frmSelectProcess.ProcessName;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtGameName.Text.Trim()))
            {
                MessageBox.Show(this, $"游戏名称不能为空.", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtGameName.Focus();
                return;
            }

            if (string.IsNullOrEmpty(this.txtProcessName.Text.Trim()))
            {
                MessageBox.Show(this, $"游戏进程不能为空.", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.txtProcessName.Focus();
                return;
            }

            if (Project == null)
            {
                this.Text = "创建控制项目";
                Project = new GameProject
                {
                    Platform = this.cbbPlatform.SelectedIndex,
                    Name = this.txtGameName.Text,
                    ProcessName = this.txtProcessName.Text,
                    GameCategory = this.cbbGameCategory.SelectedIndex,
                };
            }
            else
            {
                this.Text = "编辑控制项目";
                Project.Platform = this.cbbPlatform.SelectedIndex;
                Project.Name = this.txtGameName.Text;
                Project.ProcessName = this.txtProcessName.Text;
                Project.GameCategory = this.cbbGameCategory.SelectedIndex;
            }

            this.DialogResult = DialogResult.OK;
        }
    }
}
